
#include "usb_device.h"

#include "usb_otg.h"
#include "rtthread.h"
#include "GQueue.h"
#include "string.h"

uint16_t _desc_str[USB_STRING_DESC_BUF_SIZE]; // String buffer
uint8_t usbSerial[25] = {0};
USBdevice usbDevice;

static rt_thread_t tid_ffbwheel;

USBdevice* GetUSBDeviceHandle(void)
{
    return &usbDevice;
}
 
 void USBdeviceInit(const tusb_desc_device_t* deviceDesc,const uint8_t (*confDesc),const usb_string_desc_t* strings)
 {
     usbDevice.desc_device = deviceDesc;
     usbDevice.desc_conf = confDesc;
     usbDevice.string_desc = strings;
 }
 
void CDC_Thread_Entry(void* parameter);
void FFBWheel_Thread_Entry(void* parameter);

//void USBdevice_Thread_Entry(void* parameter)
//{
//
//    unsigned int uid = 0;
//    //读取usb的序列号
//    uid = HAL_GetUIDw2();
//    sprintf((char*)usbSerial,"%08x",uid);
//    uid = HAL_GetUIDw1();
//    sprintf((char*)usbSerial+8,"%08x",uid);
//    uid = HAL_GetUIDw0();
//    sprintf((char*)usbSerial+16,"%08x",uid);
//
////    USBdeviceInit(&usb_devdesc_ffboard_composite,usb_cdc_hid_conf,&usb_ffboard_strings_default);
//    MX_USB_OTG_FS_PCD_Init();
////    tusb_init();
//    MX_USB_DEVICE_Init();
//
//    //创建并启动ffb wheel进程，主要是处理游戏hid消息
//    tid_ffbwheel = rt_thread_create(  "ffbwheel_thread",
//                                        FFBWheel_Thread_Entry,
//                                        RT_NULL,
//                                        FFBW_THREAD_STACK_SIZE,
//                                        FFBW_THREAD_PRIORITY,
//                                        FFBW_THREAD_TIME_SLICE);
//    if(tid_ffbwheel != RT_NULL)
//    {
//        rt_thread_startup(tid_ffbwheel);
//    }
//
//    while(1)
//    {
//        tud_task();
//    }
//}
 
uint8_t* getUsbSerial(void)
{
    return usbSerial;
}

const uint8_t* getUsbDeviceDesc(void)
{
    return (uint8_t*)usbDevice.desc_device;
}

const uint8_t* getUsbConfigurationDesc(uint8_t index)
{
    return usbDevice.desc_conf;
}

const uint8_t* getUsbHidReportDesc(void)
{
    return hidff_descriptor_report;
}
/**
 * Returns a usb formatted string from the stringtable
 */
uint16_t* getUsbStringDesc(uint8_t index,uint16_t langid)
{
    (void) langid;
    uint16_t chr_count = 0;
    if (index == 0) // Language
    {
        _desc_str[1] = usbDevice.string_desc->langId;
        chr_count = 1;
    }
    else
    {
        uint8_t* field;
        if(index == usbDevice.desc_device->iSerialNumber)
        {
            field = getUsbSerial();
        }
        else if(index == usbDevice.desc_device->iProduct)
        {
            field = (uint8_t*)usbDevice.string_desc->product;
        }
        else if(index == usbDevice.desc_device->iManufacturer)
        {
            field = (uint8_t*)usbDevice.string_desc->manufacturer;
//      }else if(index > 3 && ((index - 4) < (int)usbDevice.string_desc->interfaces.size())){
        }
        else if(index > 3 && index < 7)
        {
            field = (uint8_t*)usbDevice.string_desc->interfaces[index-4];
        }
        else
        {
            return NULL;
        }
        chr_count = (USB_STRING_DESC_BUF_SIZE-1)<strlen((const char*)field)?(USB_STRING_DESC_BUF_SIZE-1):strlen((const char*)field);
        // Convert ASCII string into UTF-16
        for(uint8_t i=0; i < chr_count; i++)
        {
          _desc_str[1+i] = field[i];
        }
    }

    // first byte is length (including header), second byte is string type
    _desc_str[0] = (TUSB_DESC_STRING << 8 ) | (2*chr_count + 2);

    return _desc_str;
}





